<?php

namespace app\service;

use app\exception\ErrnoPlus;
use app\model\AppViewModel;
use app\service\cache\AppViewCacheService;
use share\exception\ApiException;
use share\service\BaseModelService;

class AppViewService extends BaseModelService
{

    /**
     * 手动设置的优先级
     */
    CONST PRIORITY_OF_USER_SET = 0b10000;

    /**
     * 包名的优先级
     */
    CONST PRIORITY_OF_PACKAGE = 0b1000;

    /**
     * 设备端的优先级
     */
    CONST PRIORITY_OF_CLIENT_TYPE = 0b100;

    /**
     * 版本的优先级
     */
    CONST PRIORITY_OF_VERSION = 0b10;

    /**
     * 区域的优先级
     */
    CONST PRIORITY_OF_INLAND = 0b1;

    public static function getModel()
    {
        return new AppViewModel;
    }

    public static function moduleFullInfo(){
        return [
            "oss" => "云存",
            "4g" => "4G",
            "link_share"  => "设备链接分享",
            "video_tutorials" => "视频教程",
            "direct_connect" => "设备直连",
            "ap_connect" => "热点直连",
            "third_login" => "三方登录",
            "third_login/appleid" => "三方登录(appleid登录)",
            "third_login/wechat" => "三方登录(微信登录)",
            "third_login/one_touch" => "三方登录(一键登录)",
            "third_login/verify_code" => "三方登录(验证码登录)",
        ];
    }

    /**
     * 是否最合适的视图
     */
    public static function matchModule($param){
        $filter = [
            'package' => ['in',[$param['package'],'*']],
            'vendor' => $param['vendor'],
            'client_type' =>['in', [$param['clientType'],0]],
            'version' =>['in', [$param['version'],'*']],
            'inland' =>['in', [$param['inland'],0]],
        ];
        $res = AppViewCacheService::getOneByFilter($filter,['priority','DESC']);
        return $res;
    }

    /**
     * 复制数据
     */
    public static function copy($id){
        $origin = self::getBase($id);
        if(empty($origin)){
            throw new ApiException("origin recode not exists",ErrnoPlus::COMMON_ERROR);
        }
        unset($origin['id']);
        unset($origin['first_time']);
        unset($origin['modify_time']);
        $res = self::add($origin);
        AppViewCacheService::reflushAll();
        return $res;
    }

    /**
     * 编辑
     */
    public static function edit($param){
        $id = $param['id'];
        $param['client_type'] = $param['clientType'];
        unset($param['clientType']);
        $module = [];
        $allowModules = array_keys(self::moduleFullInfo());
        if(isset($param['module'])){
            foreach($param['module'] as $mod){
                if(!in_array($mod,$allowModules)){
                    throw new ApiException("unknown module [".$mod."]",ErrnoPlus::PARAM_ERROR);
                }
                $module[] = $mod;
            }    
        }
 
        $param['module'] = json_encode($module,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
        $priority = 0;
        if($param['package'] != '*'){
            $priority |= self::PRIORITY_OF_PACKAGE;
        }
        if($param['version'] != '*'){
            $priority |= self::PRIORITY_OF_VERSION;
        }
        if($param['client_type'] != 0){
            $priority |= self::PRIORITY_OF_CLIENT_TYPE;
        }
        if($param['inland'] != 0){
            $priority |= self::PRIORITY_OF_INLAND;
        }


        if(isset($param['priority']) && $param['priority'] >= SELF::PRIORITY_OF_USER_SET){
            $priority = $param['priority'];
        }

        $param['priority'] = $priority;
        if($id == 0){
            $res = self::add($param);
        }
        else{
            $res = self::update($id,$param);
        }
        AppViewCacheService::reflushAll();
        return $res;
    }

    public static function del($id){
        $res = parent::del($id);
        AppViewCacheService::reflushAll();
        return $res;
    }

    public static function buildCondition($filter){
        $res = [];
        if(isset($filter['package']) && !empty($filter['package'])){
            $res['package'] = $filter['package'];
        }

        if(isset($filter['vendor']) && !empty($filter['vendor'])){
            $res['vendor'] = $filter['vendor'];
        }

        if(isset($filter['version']) && !empty($filter['version'])){
            $res['version'] = $filter['version'];
        }

        if(isset($filter['module']) && !empty($filter['module'])){
            $res['module'] =['like','%'. $filter['module'].'%'];
        }        
        return $res;
    }
}
